﻿using DevExpress.Data.Filtering;
using DevExpress.Data.Linq;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using System.Reflection;

namespace EasyXaf.FullTextSearch;

public class FullTextContainsFunction : ICustomFunctionOperatorConvertibleToExpression
{
    public string Name => "FullTextContains";

    public object Evaluate(params object[] operands)
    {
        throw new NotImplementedException();
    }

    public Type ResultType(params Type[] operands)
    {
        return typeof(bool);
    }

    Expression ICustomFunctionOperatorConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, params Expression[] operands)
    {
        var matchMethod = typeof(MySqlDbFunctionsExtensions).GetMethod(
            nameof(MySqlDbFunctionsExtensions.Match),
            BindingFlags.Static | BindingFlags.Public,
            new Type[] { typeof(DbFunctions), typeof(string), typeof(string), typeof(MySqlMatchSearchMode) });
        return Expression.Call(matchMethod, Expression.Constant(EF.Functions), operands[0], operands[1], Expression.Constant(MySqlMatchSearchMode.Boolean));
    }
}
